package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.OpMessage;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by wangjian7 on 2016/10/25.
 */
public class OpMessageConvert {
    public static DBObject castOpMessageToDBObject(OpMessage opMessage) {
        DBObject dbo = new BasicDBObject();
        if (opMessage.getId() != null) {
            dbo.put("_id", opMessage.getId());
        }
        dbo.put("type", opMessage.getType());
        dbo.put("content", opMessage.getContent());
        dbo.put("status", opMessage.getStatus());
        if(null!=opMessage.getCreateTime()){
            dbo.put("createTime", opMessage.getCreateTime());
        }
        if(null!=opMessage.getUpdateTime()){
            dbo.put("updateTime", opMessage.getUpdateTime());
        }
        return dbo;
    }
    public static OpMessage castDBObjectToOpMessage(DBObject dbObject){
        if(dbObject!=null){
            OpMessage opMessage=new OpMessage();
            opMessage.setId(ObjectUtils.toLong(dbObject.get("_id")));
            opMessage.setType(ObjectUtils.toInteger(dbObject.get("type")));
            opMessage.setContent(ObjectUtils.toString(dbObject.get("content")));
            opMessage.setStatus(ObjectUtils.toInteger(dbObject.get("status")));
            if(dbObject.get("createTime")!=null){
                opMessage.setCreateTime(ObjectUtils.toLong(dbObject.get("createTime")));
            }
            if(dbObject.get("updateTime")!=null){
                opMessage.setUpdateTime(ObjectUtils.toLong(dbObject.get("updateTime")));
            }


            return opMessage;
        }else{
            return null;
        }
    }
}
